export default {
  namespaced: true,
  state: {
    carts: []
  },
  getters: {
    total(state) {
      return state.carts
        .reduce((total1, item) => {
          return (total1 += item.price * item.count)
        }, 0)
        .toFixed(2)
    }
  },
  mutations: {
    SET_CART(state, paylord) {
      let target = state.carts.find((item) => item.id === paylord.id)
      if (target) {
        target.count++
      } else {
        let obj = {
          title: paylord.title,
          count: 1,
          id: paylord.id,
          price: paylord.price
        }
        state.carts.push(obj)
      }
    },
    DECREASE_CART(state, paylord) {
      if (paylord.count === 1) {
        state.carts = state.carts.filter((a) => {
          return a.id !== paylord.id
        })
      } else {
        // let i = state.carts.find((a) => {
        //   return a.id == paylord.id
        // })
        // i.count--
        paylord.count--
      }
    }
  }
}
